/*
 * 弹框 编辑mixin
 */
import commonMixin from "@/mixins/common"
import request from "@/utils/request"
const mixin = {
  mixins: [commonMixin],
  data() {
    return {
      row: {},
      // 编辑模式
      isEdit: false,
      // 人员管理查看详情状态
      lookSee: false,
      // 模态窗状态
      dialogVisible: false,
      // 确定按钮loading
      loadingSubmit: false,
      // 接口
      apiKey: {
        search: "", // 查询
        add: "", // 添加
        put: "", // 修改
      },
      // 表单数据
      formData: {},
      // 保存请求过来的数据
      searchValue: {},
    }
  },
  // created() {
  //   this.resetFormData()
  // },
  methods: {
    // 设置表单对象的值
    // resetFormData() {
    //   this.formData = {}
    // },
    // 查询单条数据
    async getSearch(bln, date) {
      try {
        if (bln) {
          if (date) {
            // 调用接口
            let res = await request(this.apiKey.search + this.row.id, "GET", { enterTimeStart: date.start, enterTimeEnd: date.end })
            this.searchValue = res
          } else {
            let ary = []
            if (this.row.employeeId) {
              ary = [{ key: 'employeeId', value: this.row.employeeId}]
            } else {
              ary = [{ key: 'carId', value: this.row.carId}]
            }
            // 调用接口
            let res = await request(this.apiKey.search, "post", { pageIndex: 1, pageSize: 10, search: ary })
            // 详情会用到
            this.searchValue = res
          }
        } else {
          // 调用接口
          let res = await request(this.apiKey.search + this.row.id, "GET", null)
          this.searchValue = res.data
          // 这里展示的是表单数据
          let formKeys = Object.keys(this.formData)
          formKeys.forEach((item) => {
            this.formData[item] = res.data[item]
          })
          // 编辑时保存ID
          this.formData.id = res.data.id
        }
      } catch (err) {
        console.error("reqFromData err: ", err)
      }
      this.$_loadingPageClose()
    },
    // 打开模态窗
    async open(row, bln = false, see = false, date = false) {
      // 改变模态窗状态
      this.dialogVisible = true
      this.resetFormData()
      // 是否有数据
      if (row) {
        // 开启编辑模式
        this.isEdit = true
        // 人员管理查看详情状态
        this.lookSee = see
        // 保存数据
        this.row = row
        this.$_loadingPageOpen()
        // 查询编辑的数据
        await this.getSearch(bln, date)
        // 设置编辑模式
        await this.$store.dispatch("user/setEdit", {
          status: true,
          id: row.id,
        })
      } else {
        // 关闭编辑模式
        this.isEdit = false
        // 人员管理查看详情状态
        this.lookSee = see
        // 不是编辑模式时不保存数据
        this.row = {}
        // 设置编辑模式
        await this.$store.dispatch("user/setEdit", { status: false, id: null })
      }

      this.$nextTick(() => {
        if (this.$refs.RefForm) {
          this.$refs.RefForm.clearValidate()
        }
      })
    },
    // 关闭模态窗
    close() {
      this.row = {}
      this.searchValue = {}
      if (this.imgUrl) {
        this.imgUrl = ''
      }
      this.formData = {}
      if (this.$refs.RefForm) {
        this.$refs.RefForm.resetFields()
      }
      // Object.assign(this.$data, this.$options.data.call(this))
      // 变为添加模式
      this.isEdit = false
      // 关闭模态窗
      this.dialogVisible = false
    },
    // 格式化提交的数据
    formatFormData() {
      return this.formData
    },
    // 提交数据
    async reqFromData() {
      try {
        let msg = "新增"
        if (this.isEdit) {
          // 修改接口
          msg = "修改"
          await request(this.apiKey.put + this.row.id, "PUT", this.formData)
        } else {
          // 添加接口
          if (this.apiKey.add === "/Attendance") {
            this.formData = {
              EmployeeId: this.formData.id,
              AttendanceType: this.formData.AttendanceType,
              EnterTime: this.formData.EnterTime,
              LeaveTime: this.formData.LeaveTime
            }
          }
          
          await request(this.apiKey.add, "post", this.formData)
        }
        // 关闭loading
        this.loadingSubmit = false
        // 成功提示
        this.$message({
          type: "success",
          message: `${msg}成功`,
        })
        // 关闭弹窗
        this.close()
        // 调用父组件查询列表函数
        this.$emit("success")
      } catch (err) {
        this.loadingSubmit = false
        if (this.isEdit) {
          this.$message.error('修改失败！')
        } else {
          this.$message.error('新增失败！')
        }
        console.error("reqFromData err: ", err)
      }
    },
    // 点击提交
    handleSubmit() {
      this.loadingSubmit = true
      // 验证表单
      this.$refs.RefForm.validate((valid) => {
        if (valid) {
          this.reqFromData()
        } else {
          this.loadingSubmit = false
        }
      })
    },
  },
}
export default mixin
